package com.example.demo.listener;

import com.example.demo.listener.task.Plan;
import com.example.demo.listener.task.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;

/**
 * <p>Description: spring serlet容器启动监听</p>
 *
 * @author Eric Lee
 * @version v1.0.0
 * @since 2021/8/2 16:04
 **/

@Slf4j
@Service
public class SystemStartListener implements InitializingBean, ServletContextAware {
    @Autowired
    private TaskService taskService;

    @Override
    public void afterPropertiesSet() throws Exception {

    }

    @Override
    public void setServletContext(ServletContext arg0) {
        log.info("-------------------System starting--------------------");
        try {
            Plan plan = new Plan();
            plan.setId(1);
            plan.setName("onlineDev-test");
            plan.setGroup("group");
            plan.setCron_expression("0 */1 * * * ?");//每隔1分钟执行一次
            plan.setDesc("Trigger time:Execute every two hours");
            plan.setIsTrue("1");
            plan.setJobClass("com.example.demo.listener.OnlineEquipmentPublicJob");
            try {
                log.info("plan : " + plan);
                taskService.addJob(plan);
                log.info("--Added online device task successfully-----");
            } catch (Exception e) {
                log.info("---Add student online device task exception-----");
                e.printStackTrace();
            }
        } catch (Exception e) {
            log.info("-----Task initialization failed------");
            e.printStackTrace();
        }
    }

}
